package com.mbs.project.mbs.mbinfo.controller;

import java.util.List;

import com.mbs.common.utils.poi.ExcelUtil;
import com.mbs.framework.aspectj.lang.annotation.Log;
import com.mbs.framework.aspectj.lang.enums.BusinessType;
import com.mbs.framework.web.controller.BaseController;
import com.mbs.framework.web.domain.AjaxResult;
import com.mbs.framework.web.page.TableDataInfo;
import com.mbs.project.system.user.domain.User;
import com.mbs.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mbs.project.mbs.mbinfo.domain.MessageBoardInfo;
import com.mbs.project.mbs.mbinfo.service.IMessageBoardInfoService;

/**
 * 留言板Controller
 *
 * @author xyl
 * @date 2022-01-25
 */
@Controller
@RequestMapping("/mbs/mbinfo")
public class MessageBoardInfoController extends BaseController {
    private String prefix = "mbs/mbinfo";

    @Autowired
    private IMessageBoardInfoService messageBoardInfoService;
    @Autowired
    private IUserService userService;

    @RequiresPermissions("mbs:mbinfo:view")
    @GetMapping()
    public String mbinfo(ModelMap mmap) {
        //查询业务员
        List<User> userListByRoleId = userService.getUserListByRoleId(2);
        mmap.put("disposer",userListByRoleId);
        return prefix + "/mbinfo";
    }
    @RequiresPermissions("mbs:mbinfo:user:view")
    @GetMapping("/user")
    public String mbinfoUser() {
        return prefix + "/user";
    }
    @RequiresPermissions("mbs:mbinfo:verifier:view")
    @GetMapping("/verifier")
    public String mbinfoVerifier() {
        return prefix + "/verifier";
    }

    /**
     * 查询留言板列表
     */
    @RequiresPermissions("mbs:mbinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MessageBoardInfo messageBoardInfo) {
        startPage();
        List<MessageBoardInfo> list = messageBoardInfoService.selectMessageBoardInfoList(messageBoardInfo);
        return getDataTable(list);
    }

    /**
     * 查询留言板列表
     */
    @RequiresPermissions("mbs:mbinfo:user:list")
    @PostMapping("/user/list")
    @ResponseBody
    public TableDataInfo userList(MessageBoardInfo messageBoardInfo) {
        startPage();
        User sysUser = getSysUser();
//        if(sysUser.isUser()){
            messageBoardInfo.setUserId(sysUser.getUserId());
//        }
        List<MessageBoardInfo> list = messageBoardInfoService.selectMessageBoardInfoList(messageBoardInfo);
        return getDataTable(list);
    }

    /**
     * 查询留言板列表
     */
    @RequiresPermissions("mbs:mbinfo:verifier:list")
    @PostMapping("/verifier/list")
    @ResponseBody
    public TableDataInfo verifierList(MessageBoardInfo messageBoardInfo) {
        startPage();
        User sysUser = getSysUser();
//        if(sysUser.isUser()){
            messageBoardInfo.setVerifierId(sysUser.getUserId());
//        }
        List<MessageBoardInfo> list = messageBoardInfoService.selectMessageBoardInfoList(messageBoardInfo);
        return getDataTable(list);
    }

    /**
     * 导出留言板列表
     */
    @RequiresPermissions("mbs:mbinfo:export")
    @Log(title = "留言板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MessageBoardInfo messageBoardInfo) {
        List<MessageBoardInfo> list = messageBoardInfoService.selectMessageBoardInfoList(messageBoardInfo);
        ExcelUtil<MessageBoardInfo> util = new ExcelUtil<MessageBoardInfo>(MessageBoardInfo.class);
        return util.exportExcel(list, "mbinfo");
    }

    /**
     * 新增留言板
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("email",getEmail());
        return prefix + "/add";
    }
    /**
     * 详情页面
     */
    @GetMapping("/detail/{mbId}")
    public String detail(@PathVariable("mbId") Long mbId,ModelMap mmap) {
        mmap.put("mbi",messageBoardInfoService.selectMessageBoardInfoById(mbId));
        return prefix + "/detail";
    }

    /**
     * 新增保存留言板
     */
    @RequiresPermissions("mbs:mbinfo:add")
    @Log(title = "留言板", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MessageBoardInfo messageBoardInfo) {
        messageBoardInfo.setUserId(getUserId());
        messageBoardInfo.setCreateBy(getLoginName());
        return toAjax(messageBoardInfoService.insertMessageBoardInfo(messageBoardInfo));
    }

    /**
     * 修改留言板
     */
    @GetMapping("/edit/{mbId}")
    public String edit(@PathVariable("mbId") Long mbId, ModelMap mmap) {
        MessageBoardInfo messageBoardInfo = messageBoardInfoService.selectMessageBoardInfoById(mbId);
        mmap.put("messageBoardInfo", messageBoardInfo);
        mmap.put("email",getEmail());
        return prefix + "/edit";
    }

    /**
     * 修改保存留言板
     */
    @RequiresPermissions("mbs:mbinfo:edit")
    @Log(title = "留言板", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MessageBoardInfo messageBoardInfo) {
        messageBoardInfo.setUpdateBy(getLoginName());
        return toAjax(messageBoardInfoService.updateMessageBoardInfo(messageBoardInfo));
    }

    /**
     * 删除留言板
     */
    @RequiresPermissions("mbs:mbinfo:remove")
    @Log(title = "留言板", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(messageBoardInfoService.deleteMessageBoardInfoByIds(ids));
    }
}
